# -*- coding: utf8 -*-

import socket
import psutil
import os
import logging
import lorun
import hashlib

from exception import JudgeClientError

JUDGER_WORKSPACE = "/judger/run"
LOG_BASE = "/judger/log"

TEST_CASE_DIR = "/test_case"

SERVER_LOG_PATH = os.path.join(LOG_BASE, "judge_server.log")


RESULT_STR = [
    'Accepted',
    'Presentation Error',
    'Time Limit Exceeded',
    'Memory Limit Exceeded',
    'Wrong Answer',
    'Runtime Error',
    'Output Limit Exceeded',
    'Compile Error',
    'System Error'
]


spj_config = {
  'exe_name': 'spj',
  'command': 'g++ -std=c++11 {src} -lm -o {exe} >{err} 2>&1'
}


class ProblemIOMode:
  standard = "Standard IO"
  file = "File IO"


logger = logging.getLogger(__name__)
handler = logging.FileHandler(SERVER_LOG_PATH)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.WARNING)


def server_info():
    ver = lorun.version()
    return {
        "hostname": socket.gethostname(),
        "cpu": psutil.cpu_percent(),
        "cpu_core": psutil.cpu_count(),
        "memory": psutil.virtual_memory().percent,
        "judger_version": "lorun " + ".".join([str((ver >> 16) & 0xff), str((ver >> 8) & 0xff), str(ver & 0xff)])
		}


def get_token():
    token = os.environ.get("TOKEN")
    if token:
        return token
    else:
        raise JudgeClientError("env 'TOKEN' not found")

token = hashlib.sha256(get_token().encode("utf-8")).hexdigest()